using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Usercontrols_SelectMenuItem : System.Web.UI.UserControl
{
    public bool IsActive
    {
        get
        {
            return CheckBoxBindToItem.Checked;
        }
    }

    public bool EnableSelection
    {
        get
        {
            if(ViewState["EnableSelection"] == null)
                return true;
            return (bool) ViewState["EnableSelection"];
        }
        set
        {
            ViewState["EnableSelection"] = value;
        }
    }

    public void Reset()
    {
        DropDownList1.SelectedIndex = -1;
        DropDownList2.SelectedIndex = -1;

        CheckBoxBindToItem.Checked = false;
        DropDownList1.Enabled = EnableSelection && CheckBoxBindToItem.Checked;
        DropDownList2.Enabled = EnableSelection && CheckBoxBindToItem.Checked;

        DataBind();
    }

    private List<int> _excludedItems;
    public List<int> ExcludeMenuItems
    {
        get
        {
            _excludedItems = ViewState["ExcludeMenuItems"] as List<int>;
            if (_excludedItems == null)
                _excludedItems = new List<int>();
            return _excludedItems;
        }
        set
        {
            ViewState["ExcludeMenuItems"] = _excludedItems = value;
        }
    }

    public void UpdateExcludedItems()
    {
        ListItemCollection removeItems = new ListItemCollection();
        foreach(ListItem item in DropDownList2.Items)
        {
            if(ExcludeMenuItems.Contains(Convert.ToInt32(item.Value)))
            {
                removeItems.Add(item);
            }
        }

        foreach(ListItem item in removeItems)
            DropDownList2.Items.Remove(item);
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if(bindUnusedItems)
            odsMenuItemSelection.SelectMethod = "GetUnusedMenuItems";
    }

    private bool bindUnusedItems = false;
    public bool BindUnunsedMenuItems
    {
        set
        {
            bindUnusedItems = value;
        }
    }

    public Blazing.BusinessObjects.Cms.MenuItem MenuItem
    {
        get
        {
            if (!string.IsNullOrEmpty(DropDownList2.SelectedValue))
                return Blazing.BusinessObjects.Cms.MenuItem.Load(int.Parse(DropDownList2.SelectedValue));
            return null;
        }
        set
        {
            if (value != null)
            {
                Blazing.BusinessObjects.Cms.Menu boundMenu = value.Menu;
                if (value.Menu == null)
                    boundMenu = Blazing.BusinessObjects.Cms.MenuItem.Load(value.ID).Menu;

                DropDownList1.DataBind();
                DropDownList1.SelectedValue = boundMenu.ID + "";
                
                DropDownList2.DataBind();
                DropDownList2.SelectedValue = value.ID + "";

                CheckBoxBindToItem.Checked = true;
                DropDownList1.Visible = EnableSelection && CheckBoxBindToItem.Checked;
                DropDownList2.Visible = EnableSelection && CheckBoxBindToItem.Checked;

                LiteralMenuName.Text = string.Format("<b>Menu:</b> {0}", boundMenu.Name);
                LiteralMenuItemName.Text = string.Format("<b>Item:</b> {0}", value.Label);
                LiteralMenuName.Visible = !DropDownList1.Visible;
                LiteralMenuItemName.Visible = !DropDownList2.Visible;

            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Enabled = EnableSelection && CheckBoxBindToItem.Checked;
        DropDownList2.Enabled = EnableSelection && CheckBoxBindToItem.Checked;
    }
    protected void CheckBoxBindToItem_CheckedChanged(object sender, EventArgs e)
    {
        DropDownList1.Enabled = EnableSelection && CheckBoxBindToItem.Checked;
        DropDownList2.Enabled = EnableSelection && CheckBoxBindToItem.Checked;
        DataBind();

        if (!EnableSelection && !CheckBoxBindToItem.Checked)
        {
            CheckBoxBindToItem.Visible = false;
            DropDownList1.Visible = false;
            DropDownList2.Visible = false;
            LiteralMenuName.Visible = false;
            LiteralMenuItemName.Visible = false;
        }
    }

    protected void DropDownList2_DataBound(object sender, EventArgs e)
    {
        UpdateExcludedItems();
    }
}
